package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.SkuInfo;
import com.atguigu.gmall.product.service.SkuInfoService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author dyqstart
 * @create 2021-08-05-22:58
 * @since 1.0.0
 */
@RestController
@RequestMapping("/admin/product")
@Slf4j
public class SkuController {

    @Autowired
    SkuInfoService skuInfoService;

    //http://192.168.200.1:88/admin/product/saveSkuInfo
    /**
     * {
     *     "id":null,
     *     "spuId":4,
     *     "price":"1999",
     *     "skuName":"小米3",
     *     "weight":"1",
     *     "skuDesc":"小米3",
     *     "category3Id":61,
     *     "skuAttrValueList":[
     *         {
     *             "attrId":"1",
     *             "valueId":"57"
     *         },
     *        …
     *     ],
     *     "skuSaleAttrValueList":[
     *         {
     *             "saleAttrValueId":"18",
     *             "saleAttrValueName":"红色",
     *             "baseSaleAttrId":"1",
     *             "saleAttrName":"选择颜色"
     *         }
     *     ],
     *     "skuImageList":[
     *         {
     *             "spuImgId":33,
     *             "imgName":"list.jpg",
     *             "imgUrl":"http://47.93.118.241/group1/M00/00/00/L1128V47i9GARYUWAADxtUrmGBA961.jpg",
     *             "isDefault":0
     *         }
     *     ],
     *     "skuDefaultImg":null,
     *     "tmId":4
     * }
     */

    @PostMapping("saveSkuInfo")
    public Result saveSkuInfo(@RequestBody SkuInfo skuInfo){

        log.info("sku保存信息：{}",skuInfo);

        skuInfoService.bigSaveInfo(skuInfo);

        return Result.ok();
    }


    //http://api.gmall.com/admin/product/list/{page}/{limit}
    @GetMapping("list/{page}/{limit}")
    public Result<Page<SkuInfo>> listSkuInfo(@PathVariable("page") Long page,
                                                   @PathVariable("limit") Long limit){
        Page<SkuInfo> infoPage = new Page<>(page, limit);
        Page<SkuInfo> skuInfoPage = skuInfoService.page(infoPage);

        return Result.ok(skuInfoPage);
    }


    @GetMapping("onSale/{skuId}")
    public Result onSale(@PathVariable("skuId") Long skuId){

        boolean b = skuInfoService.onSale(skuId);

        return Result.ok();
    }

    @GetMapping("cancelSale/{skuId}")
    public Result cancelSale(@PathVariable("skuId") Long skuId){

        boolean b = skuInfoService.cancelSale(skuId);

        return Result.ok();
    }
}
